/**
 * Purpose of this function is to call "createEmbedLinks" function
 * on the first message in group. For other messages the "createEmbedLinks" 
 * function is called directly after grouping.
 */
function startEmbeding() {
    // Get the message by it's id
    var message = document.getElementById("toBeAnalyzed");
    if (message == null) { return; }
    // We don't want this message to be analyzed more then
    // once, so we set its id to nothing.
    message.id = null;
    // and finaly call the createEmbedLinks function
    createEmbedLinks(message);
}

/**
 * This function searches for hyperlinks in given
 * message. Then it uses specified functions to find
 * if the hyperlink leads to an image, flash video etc.
 */
function createEmbedLinks(/*Element*/message) 
{
    // Searches for all hyperlinks. IEView automaticly
    // sets the class of hyperlinks to be "link".
    // That means that we can search for them by className.
    var links = document.links; //getElementsByClassName("link", "a", message);
    
    // Let's test all hyperlinks, if any of them matches 
    // the specified pattern. (for image, video, etc.)
    for (var i = 0; i < links.length; i++) {
        var link = links[i];

        if (link.skip)
            continue;

        link.skip = true;
        // all next functions search for pattern, that
        // matches images/videos. And if they find a 
        // match, they add a clickable text after the hyperlink.
        processImage(link, message);
        processYouTube(link, message);
    }
}